<template>
	<z-paging ref="paging" v-model="dataList" @query="queryList"
		:loading-more-custom-style="{ paddingBottom: 'env(safe-area-inset-bottom)' }">
		<template #top>
			<view class="zpagin_top">
				<nav-bar :iconLeft="true" />
				<view class="search">
					<custom-search v-model="queryParams.search" @search="refreshList" placeholder="请输入设备名称/设备编号" />
					<uni-data-select style="z-index: 50;" placeholder="请选择设备类型" v-model="queryParams.classify_id"
						:localdata="classify_list" @change="refreshList" />
				</view>
				<dui-float-ball :pathUrl="`${pageUrl}/compile`" v-if="$hasPerm(['110201'])" />
			</view>
		</template>
		<up-swipe-action>
			<up-swipe-action-item v-for="item in dataList" :key="item.id" :item-data="item"
				:name="JSON.stringify(swipeActPer(options)) + '&' + item.code + '&' + item.id + '&' + item.name"
				:options="swipeActPer(options)" @click="action">
				<uni-card margin="0" spacing="0" @click="goDetail(item, pageUrl, '110204')">
					<template v-slot:title>
						<uni-list>
							<uni-list-item :title="item.code" :note="item.name" thumbSize="lg">
								<template v-slot:footer>
									<up-tag size="mini" :text="status_list[item.status_display].text" plain plainFill
										:type="status_list[item.status_display].type" />
								</template>
							</uni-list-item>
						</uni-list>
					</template>
					<view class="con">
						<view class="ctent">设备类型：{{ item.classify_name }}</view>
						<view class="ctent">设备型号：{{ item.model }}</view>
					</view>
					<view class="con">
						<view class="ctent">供应商：{{ item.supplier }}</view>
						<view class="ctent">启用日期：{{ item.use_start_date }}</view>
					</view>
					<view class="con" style="align-items: normal;">
						<view class="ctent" style="flex: none;">备注：</view>
						<view class="ctent">{{ item.desc }}</view>
					</view>
				</uni-card>
			</up-swipe-action-item>
		</up-swipe-action>
	</z-paging>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import deDeviceApi from '../../../api/device/de_list'
import { goDetail, swipeActPer } from '@/utils/permission'

//作用于新增或编辑后刷新页面数据
uni.$bus.useBusOn('de_list', refreshList);

//页面header标题

onLoad(option => {
	get_classify_list()
})
const status_list = {
	'已报废': { text: '已报废', type: 'info' },
	'闲置': { text: '闲置', type: 'warning' },
	'运行中': { text: '运行中', type: 'primary' },
	'维修中': { text: '维修中', type: 'error' },
}
//滑动单元格按钮
const options = [
	{
		text: '修改', per: '110202',
		style: {
			backgroundColor: '#0081ff',
		}
	}, {
		text: '删除', per: '110203',
		style: {
			backgroundColor: '#f56c6c'
		}
	}
];
//页面跳转路径
const pageUrl = '/views/device/de_list';


//查询参数
const queryParams = reactive({
});

//获取paging实例
const paging = ref(null)
const dataList = ref([])
//列表数据方法
function queryList(pageNo, pageSize) {
	const params = {
		page: pageNo,
		page_size: pageSize,
		...queryParams
	}
	deDeviceApi.get_device(params).then((res) => {
		paging.value?.complete(res.results);
	})
}

const classify_list = ref([]);
function get_classify_list() {
	deDeviceApi.get_device_classify({ page: 1, page_size: 99999 }).then(res => {
		classify_list.value = res.results.map(v => ({
			text: v.name,
			value: v.id
		}))
	})
};

//滑动操作按钮
function action(v) {
	const options = JSON.parse(v.name.split("&")[0])
	const code = v.name.split("&")[1]
	const id = v.name.split("&")[2]
	const name = v.name.split("&")[3]
	const index = v.index
	if (options[index].text == '修改') {
		uni.navigateTo({
			url: `${pageUrl}/compile?type=edit&id=${id}`
		})
	}
	if (options[index].text == '删除') {
		delete_type(code, id, name)
	}
};

//删除操作
function delete_type(code, id, name) {
	uni.showModal({
		title: '提示',
		content: `确认要删除编号为【${code}】的设备吗？`,
		success: (res) => {
			if (res.confirm) {
				deDeviceApi.delete_device(id).then((res) => {
					uni.$u.toast('删除成功')
					setTimeout(() => {
						refreshList();
					}, 500)
				})
			}
		}
	});
};

//刷新列表数据方法
function refreshList() {
	paging.value?.reload()
};
</script>

<style lang="scss"></style>